package com.dw.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dw.apis.UserClient;
import com.dw.article.mapper.mp.ReplyInfoMapper;
import com.dw.article.service.CommentUserService;
import com.dw.article.service.ReplyUserService;
import com.dw.common.entitys.BaseResponse;
import com.dw.model.article.entitys.ReplyInfo;
import com.dw.article.service.ReplyInfoService;
import com.dw.model.article.vos.comment.QueryCommentVO;
import com.dw.model.article.vos.reply.QueryReplyVO;
import com.dw.model.user.vos.GetUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 回复信息表(ReplyInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-11-08 08:49:28
 */
@Service("replyInfoService")
public class ReplyInfoServiceImpl extends ServiceImpl<ReplyInfoMapper, ReplyInfo> implements ReplyInfoService {

    @Resource
    private ReplyUserService replyUserService;

    @Resource
    private UserClient userClient;

    @Override
    public List<QueryReplyVO> queryReply(List<ReplyInfo> replyInfoList) {
        return replyInfoList.stream().map(item -> {
            Long replyId = item.getLeiId();
            // 查询评论点赞数量
            QueryReplyVO queryReplyVO = new QueryReplyVO();
            Integer likeCount = replyUserService.getReplyLikeCount(replyId);
            // 拷贝基本数据
            BeanUtils.copyProperties(item, queryReplyVO);
            // 拷贝点赞数量
            queryReplyVO.setLikeCount(likeCount);
            // 获取登录用户与文章之间的点赞状态
            Integer like = replyUserService.isLike(replyId);
            queryReplyVO.setIsLiked(like);
            // 获取评论发布者的头像和昵称
            BaseResponse<GetUserVO> getUserVO = userClient.getUserName(item.getCreatedBy());
            queryReplyVO.setAvatar(getUserVO.getData().getAvatar());
            queryReplyVO.setNickName(getUserVO.getData().getNickname());
            // 将雪花算法生成的id转为字符串，便于前端接收
            queryReplyVO.setLeiId(String.valueOf(replyId));
            queryReplyVO.setCreatedBy(String.valueOf(item.getCreatedBy()));
            return queryReplyVO;
        }).collect(Collectors.toList());
    }
}

